﻿// <copyright file="EasyWebApp.cs" company="<Customer Name>">This is a "work-for-hire".</copyright><author><Class Author's Name></author>

/*
 *      Title:    <Class Description>
 *      Module:   ClassTemplate (<Namespace>:ClassTemplate.cs)
 *      Project:  <Project Name>
 *      Customer: <Customer Name>
 *      Facility: MicroCODE Incorporated
 *      Date:     <Class Creation Date>
 *      Author:   <Class Author's Name>
 * 
 * 
 * 
 *  
 *      MODIFICATIONS:
 *      --------------
 *
 *  Date:         By-Group:   Rev:     Description:
 *
 *  DD-Mmm-YYYY   XYZ-GROUP  {0001}    Describe reason for creating or modifying class... 
 * 
 * 
 * 
 */
using System.Collections.Specialized;
using System.Data;
using Org.EasyWeb.ConfigXml;
using Org.EasyWeb.DataAccess;
using Org.EasyWeb.Exception;
using Org.EasyWeb.Provider;
using Org.EasyWeb.Sys;

namespace Org.EasyWeb.Data
{   
    [XmlDataSet(WebMultiMDTreeXmlDataSet.REG_NAME, Description = "树结构的多主表，多从表",
        Author = "AMen", CreateDate = "2011-03-04")]
    public class WebMultiMDTreeXmlDataSet : WebMultiMD2XmlDataSet
    {
        // Fields
        internal const string REG_NAME = "MultipleMasterDetailTree2";
        private TreeHelper fHelper;

        // Methods
        public WebMultiMDTreeXmlDataSet(ModuleXml2File module)
            : base(module)
        {
            base.DisablePage = DisableFunction.List;
            this.fHelper = new TreeHelper(this, module);
        }

        protected override void CommittingData()
        {
            base.CommittingData();
            this.fHelper.CommittingData();
        }

        protected override void Delete()
        {
            this.fHelper.Delete();
            foreach (TableResolver resolver in base.MasterResolvers)
            {
                if (resolver != base.MainResolver)
                {
                    resolver.Delete();
                }
            }
            if (base.CascadeDeletes)
            {
                foreach (TableResolver resolver2 in base.DetailResolvers)
                {
                    resolver2.Delete();
                }
            }
            base.Commit();
        }

        protected override void FillUpdateTables(object key, PageStyle style, NameValueCollection queryString)
        {
            if (style == PageStyle.Delete)
            {
                TableResolver mainResolver = base.MainResolver;
                string tableName = mainResolver.TableName;
                if (base.FillingUpdateArgs.Handled[mainResolver])
                {
                    try
                    {
                        if (mainResolver.KeyCount == 1)
                        {
                            this.SelectWithParams(mainResolver, tableName + "_COPY", mainResolver.KeyFieldArray, new object[] { key });
                        }
                        else
                        {
                            object[] values = new object[mainResolver.KeyCount];
                            int num = 0;
                            foreach (string str2 in mainResolver.KeyFieldArray)
                            {
                                values[num++] = queryString[str2];
                            }
                            this.SelectWithParams(mainResolver, tableName + "_COPY", mainResolver.KeyFieldArray, values);
                        }
                    }
                    catch (System.Exception exception)
                    {
                        throw exception;
                    }
                    if (mainResolver.HostTable.Rows.Count == 0)
                    {
                        throw new NoRecordException();
                    }
                }
                base.MasterRelations.FillDetailTable(base.FillingUpdateArgs.Handled);
                base.DetailRelations.FillDetailTable(base.FillingUpdateArgs.Handled);
                mainResolver.TableName = tableName;
            }
            else
            {
                base.FillUpdateTables(key, style, queryString);
            }
        }

        protected override string GetDataXslt(PageStyle style, string operation)
        {
            string dataXslt = this.fHelper.GetDataXslt(style, operation);
            if (dataXslt == string.Empty)
            {
                return base.GetDataXslt(style, operation);
            }
            return dataXslt;
        }

        public override string GetDefaultPage(bool isPost, PageStyle style, string operation, PageX pageX, string retURL)
        {
            return this.fHelper.GetDefaultPage(isPost, style, operation, pageX, retURL);
        }

        public override string GetDefaultXsltTemplate(PageStyle style, string operation)
        {
            string defaultXsltTemplate = this.fHelper.GetDefaultXsltTemplate(style, operation);
            if (defaultXsltTemplate == string.Empty)
            {
                return base.GetDefaultXsltTemplate(style, operation);
            }
            return defaultXsltTemplate;
        }

        public override bool GetSupportDoubleTransform(PageStyle style, string operation)
        {
            return this.fHelper.GetSupportDoubleTransform(style, operation);
        }

        public override string GetXsltFile(bool isIe, PageStyle style, string operation)
        {
            return this.fHelper.GetXsltFile(isIe, style, operation);
        }

        protected override void NavigateData(PageStyle style)
        {
            if (style != PageStyle.Custom)
            {
                base.NavigateData(style);
            }
        }

        protected override void ProcessCodeTables(PageStyle style)
        {
            if (style != PageStyle.Custom)
            {
                base.ProcessCodeTables(style);
            }
        }

        private void SelectWithParams(TableSelector selector, string tableName, string field, object value)
        {
            this.SelectWithParams(selector, tableName, new string[] { field }, new object[] { value });
        }

        private void SelectWithParams(TableSelector selector, string tableName, string[] fields, params object[] values)
        {
            SQLBuilder.GetSelectCommandWithParams(selector, base.DbConnection, fields);
            for (int i = 0; i < fields.Length; i++)
            {
                IDataParameter parameter = (IDataParameter)selector.DataAdapter.SelectCommand.Parameters[GlobalProvider.GetParamName(fields[i])];
                parameter.DbType = DbType.AnsiString;
                parameter.Value = values[i];
            }
            selector.TableName = tableName;
            selector.FillDataSet();
        }

    }
}
